<?php

	/*
	 * The settings page
	 * Basic settings for the current party etc are stored through this page.
	 * All get stored in the database. No clicks needed, immediate saves.
	 * Page is also used to get an overview of installed sponsor SWF files on the server and to upload new ones.
	 *
	 * Variables:
	 *
	 * Returns: HTML page
	 *
	 */

	$script = '
	$(document).ready(function(){
		$.extend(DateInput.DEFAULT_OPTS, {
		  stringToDate: function(string) {
		    var matches;
		    if (matches = string.match(/^(\d{4,4})-(\d{2,2})-(\d{2,2})$/)) {
		      return new Date(matches[1], matches[2] - 1, matches[3]);
		    } else {
		      return null;
		    };
		  },

		  dateToString: function(date) {
		    var month = (date.getMonth() + 1).toString();
		    var dom = date.getDate().toString();
		    if (month.length == 1) month = "0" + month;
		    if (dom.length == 1) dom = "0" + dom;
		    return date.getFullYear() + "-" + month + "-" + dom;
		  }
		});
		$($.date_input.initialize);


		$(":text").change(function(){
			$.get("'.$url_prefix.'settings/save/&n="+this.name+"&val="+this.value);
		});
		$(":radio").change(function(){
			$.get("'.$url_prefix.'settings/save/&n="+this.name+"&val="+this.value);
		});
	});


	function ajaxFileUpload()
	{
		//starting setting some animation when the ajax starts and completes
		$("#smsloadlistindicator")
		.ajaxStart(function(){
			$(this).show();
		})
		.ajaxComplete(function(){
			$(this).hide();
		});

		/*
			prepareing ajax file upload
			url: the url of script file handling the uploaded files
            fileElementId: the file type of input element id and it will be the index of  $_FILES Array()
			dataType: it support json, xml
			secureuri:use secure protocol
			success: call back function when the ajax complete
			error: callback function when the ajax failed

                */
		$.ajaxFileUpload
		(
			{
				url:"'.$url_prefix.'ajax/handlesponsorupload/",
				secureuri:false,
				fileElementId:"fileToUpload",
				dataType: "json",
				success: function (data, status)
				{
					if(typeof(data.error) != "undefined")
					{
						if(data.error != \'\')
						{
							alert(data.error);
						}else
						{
							alert(data.msg);
						}
					}
				},
				error: function (data, status, e)
				{
					alert(e);
				}
			}
		)

		return false;
	}
	';

	htmlheader($script);

	echo '
	<div id="lpart">
		<h3>Party information</h3>
		<div id="fwords" class="bigboxcover bigbox">
			<table>
				<tr><td>Date</td><td><input type="text" class="date_input" style="text-align:center; width: 80px;" name="pdate" value="'.$settings["party_date"].'" /> YYYY-MM-DD</td></tr>
				<tr><td>Hour</td><td><input type="text" class="" style="text-align:center; width: 40px;" name="phour" value="'.$settings["party_hour"].'" /> hh:mm</td></tr>
			</table>
		</div>

		<h3>System settings</h3>
		<div id="fnumbers" class="bigboxcover bigbox">
		<table>
			<tr><td><input type="radio" name="usenmrban" value="0" '.((isset($settings["ban_by_number"]) and ($settings["ban_by_number"] == 0))?'checked':'').' /> | <input type="radio" name="usenmrban" value="1" '.((isset($settings["ban_by_number"]) and ($settings["ban_by_number"] == 1))?'checked':'').' /></td><td>Use number banning</td></tr>
			<tr><td><input type="radio" name="usetxtban" value="0" '.((isset($settings["ban_by_content"]) and ($settings["ban_by_content"] == 0))?'checked':'').' /> | <input type="radio" name="usetxtban" value="1" '.((isset($settings["ban_by_content"]) and ($settings["ban_by_content"] == 1))?'checked':'').' /></td><td>Use SMS contents banning</td></tr>
			<tr><td>Delay:</td><td><input type="text" class="" style="text-align:center; width: 20px;" name="smsdelay" value="'.$settings["sms_delay"].'" /> seconds</td></tr>
			<tr><td><input type="radio" name="useoldsms" value="0" '.((isset($settings["sms_use_old"]) and ($settings["sms_use_old"] == 0))?'checked':'').' /> | <input type="radio" name="useoldsms" value="1" '.((isset($settings["sms_use_old"]) and ($settings["sms_use_old"] == 1))?'checked':'').' /></td><td>Use old SMS messages if no new ones</td></tr>
			<tr><td>Show </td><td><input type="text" class="" style="text-align:center; width: 20px;" name="smsnrrand" value="'.$settings["sms_use_old_num"].'" /> random SMS messages if no new ones</td></tr>
		</table>
		</div>

	</div>
	';


	echo '
		<div id="rpart">


		<table class="smslistcover striped" style="width:100%">
			<tr style="height:30px;"><th>Sponsors</th><th><img src="'.$url_prefix.'/images/load.gif" style="display:none; float:right" id="smsloadlistindicator" /></th></tr>
			<tr><td colspan="2" style="padding:0px;">
			<div id="sponsorlist" style="height:400px; overflow:hidden">
				<table id="runningSponsors">
		';
		$dir = opendir("sponsors/");
		while(false !== ($file = readdir($dir))){
			if(strpos($file,".swf") !== false){
				echo '<tr>
					<td><a href="#"><img src="'.$url_prefix.'/images/cross.png" /></a></td>
					<td>'.$file.'</td>
					</tr>
				';
			}
		}

		echo '
			</table>
			</div>
		</td></tr>
		</table>
		<form method="post" enctype="multipart/form-data" action="'.$url_prefix.'ajax/handlesponsorupload/">
			Add new: <input type="file" name="files" id="fileToUpload" /><button type="submit">Upload</button>
		</form>
		</div>

		<div class="editbox"></div>
	';



	htmlfooter();
?>